package edu.cs346.group5.logic;

//test

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

import edu.cs346.group5.backend.Database;
import edu.cs346.group5.datastores.User;


public class Login {
	
	public static User LoggedInUser = null;

	public static void RegisterUser(User user, int questionId, String answer, String password){
		//generate random salt
		String salt =  UUID.randomUUID().toString();
		String noPicture = "http://images2.wikia.nocookie.net/__cb20090329133961/superfriends/images/a/a5/No_Photo_Available.jpg";
		
		java.util.Date today = new java.util.Date();
		java.sql.Date sqlToday = new java.sql.Date(today.getTime());

		try {
			Database.insert("Users", "register_date, hashed_pw, salt, first_name, last_name, email, profile_picture, secret_question_id, secret_answer",
					 "'" + sqlToday + "', " + "'" + md5(password + salt) + "', " + "'" + salt + "', " + "'" + user.FirstName + "', " + "'" + user.LastName + "', " + "'" + user.Email + "', " + "'" + noPicture + "', " + "'" + questionId + "', " + "'" + answer + "'");
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	//this method will generate a hashed String for the given input string
	public static String md5(String input) {
        
        String md5 = null;
         
        if(null == input) return null;
         
        try {
             
        //Create MessageDigest object for MD5
        MessageDigest digest = MessageDigest.getInstance("MD5");
         
        //Update input string in message digest
        digest.update(input.getBytes(), 0, input.length());
 
        //Converts message digest value in base 16 (hex) 
        md5 = new BigInteger(1, digest.digest()).toString(16);
 
        } catch (NoSuchAlgorithmException e) {
 
            e.printStackTrace();
        }
        return md5;
    }
	
	public static Boolean CheckPassword(String email, String password){
		List<HashMap<String, Object>> results = null;
		try{
			results = Database.select("select * from Users where email = '" + email + "';");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		if(results.size() == 0)
		{
			//no registered user with this login
			return false;
		}
		else{
			try{
				results = Database.select("select * from Users where email = '" + email + "' AND hashed_pw = '" + md5(password + results.get(0).get("salt")) + "';");
				//System.out.println(md5(password + results.get(0).get("salt")));
			} catch (SQLException e) {
				e.printStackTrace();
			}
			if(results.size() == 0)
			{
				//no registered user with this login
				return false;
			}
			else{
				LoggedInUser = new  User();
				LoggedInUser.Email = results.get(0).get("email").toString();
				LoggedInUser.RegisterDate = (java.util.Date) results.get(0).get("register_date");
				LoggedInUser.FirstName = results.get(0).get("first_name").toString();
				LoggedInUser.LastName = results.get(0).get("last_name").toString();
				LoggedInUser.Id = (int) results.get(0).get("id");
				LoggedInUser.isAdmin=LoggedInUser.Email.equals("admin");
				LoggedInUser.ProfilePicture = results.get(0).get("profile_picture").toString();
				LoggedInUser.BanEndDate = (java.util.Date) results.get(0).get("ban_end_date");
				return true;
			}
		}
	}
	
	public static HashMap<String, Object> CheckEmail(String email){
		List<HashMap<String, Object>> results = null;
		try{
			results = Database.select("select * from Users where email = '" + email + "';");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		if(results.size() == 0)
		{
			//no registered user with this login
			return null;
		}
		else{
			return results.get(0);
		}
	}
	
	public static String GetSecurityQuestion(int id)
	{
		List<HashMap<String, Object>> results = null;
		try{
			results = Database.select("select * from SecretQuestions where id = '" + id + "';");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		if(results.size() == 0)
		{
			//no registered user with this login
			return null;
		}
		else{
			System.out.println(results.get(0).get("question").toString());
			return results.get(0).get("question").toString();
		}
	}
	
	public static void SetNewPassword(String password, int id){
		String salt =  UUID.randomUUID().toString();
		try{
			Database.execRaw("Update Users Set hashed_pw = '" + md5(password + salt) + "', salt = '" + salt + "'  Where id = '" + id +"'");
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
